package com.smcaiot.jfttask.south.adapter;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smcaiot.jfttask.domain.*;
import com.smcaiot.jfttask.south.convert.JftTaskLogsConvert;
import com.smcaiot.jfttask.south.entity.JftTaskLogs;
import com.smcaiot.jfttask.south.mapper.JftTaskLogsMapper;
import com.smcaiot.jfttask.south.port.TaskFlowRepository;
import org.springframework.stereotype.Component;

/**
 * @Author: cz
 * @Date: 2023/10/7
 * @Description:
 */
@Component
public class TaskFlowRepositoryAdapter extends ServiceImpl<JftTaskLogsMapper, JftTaskLogs> implements TaskFlowRepository {
    @Override
    public TaskFlowId saveTaskFlow(TaskId taskId, TaskBatchId taskBatchId, TaskFlowStatus taskFlowStatus) {
        Assert.notNull(taskId, "任务ID不能为空");
        Assert.notBlank(taskId.value(), "任务ID不能为空");
        Assert.notNull(taskBatchId, "子任务ID不能为空");
        Assert.notBlank(taskBatchId.value(), "子任务ID不能为空");
        Assert.notNull(taskFlowStatus, "日志状态不能为空");
        Assert.notBlank(taskFlowStatus.getCode(), "日志状态不能为空");

        TaskFlowId taskFlowId = TaskFlowId.next();
        JftTaskLogs taskLogs = new JftTaskLogs().setTaskLogId(taskFlowId.value())
                .setTaskId(taskId.value())
                .setTaskBatchId(taskBatchId.value())
                .setStatus(taskFlowStatus.getCode());
        save(taskLogs);
        return taskFlowId;
    }

    @Override
    public void deleteByTaskFlowId(TaskFlowId taskFlowId) {
        getBaseMapper().deleteByTaskLogId(taskFlowId.value());
    }

    @Override
    public TaskFlow findByTaskFlowId(TaskFlowId taskFlowId) {
        JftTaskLogs taskLogs = getBaseMapper().findByTaskLogId(taskFlowId.value());
        return JftTaskLogsConvert.INSTANCE.to(taskLogs);
    }
}
